<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <script src="http://code.jquery.com/jquery-latest.js"></script>
        <link rel="stylesheet" href="http://code.jquery.com/qunit/git/qunit.css" type="text/css" media="screen" />
        <script type="text/javascript" src="http://code.jquery.com/qunit/git/qunit.js"></script>

        <script type="text/javascript" src="morpheme-lib.js"></script>

        <script>
            $(document).ready(function(){

                module("trivial tests");

                test("Module trivial tests", function() {
                    expect(8);

                    var a;
                    var a1 = !a;
                    equal(
                        a1,
                        true, 
                        'var a; if (!a) alert("ok");'
                    );
                    var a2 = !!a;
                    equal(
                        a2,
                        false, 
                        'var a; if (!!a) alert("ko");'
                    );

                    var b = null;
                    var b1 = !b;
                    equal(
                        b1,
                        true, 
                        'var a = null; if (!a) alert("ok");'
                    );
                    var b2 = !!b;
                    equal(
                        b2,
                        false, 
                        'var a = null; if (!!a) alert("ko");'
                    );

                    var c = undefined;
                    var c1 = !c;
                    equal(
                        c1,
                        true, 
                        'var a = undefined; if (!a) alert("ok");'
                    );
                    var c2 = !!c;
                    equal(
                        a2,
                        false, 
                        'var a = undefined; if (!!a) alert("ko");'
                    );

                    var d = '';
                    var d1 = !d;
                    equal(
                        d1,
                        true, 
                        'var a = \'\'; if (!a) alert("ok");'
                    );
                    var d2 = !!d;
                    equal(
                        a2,
                        false, 
                        'var a = \'\'; if (!!a) alert("ko");'
                    );

                });

                module("Module Morpheme.utils.stripper");

                test("Module Morpheme.utils.stripper.stripScripts", function() {
                    expect(5);

                    equal(
                        Morpheme.utils.stripper.stripScripts('whatever<script>alert(2);<\/script>whatever'),
                        'whateverwhatever', 
                        "Module Morpheme.utils.stripper.stripScripts, simple script"
                    );

                    equal(
                        Morpheme.utils.stripper.stripScripts('whatever<object>whatever<\/object>whatever'),
                        'whateverwhatever', 
                        "Module Morpheme.utils.stripper.stripScripts, simple object"
                    );

                    equal(
                        Morpheme.utils.stripper.stripScripts('<p onclick="javascript:alert(1);">whatever<\/p>'),
                        '<p>whatever<\/p>', 
                        "Module Morpheme.utils.stripper.stripScripts, javascript event"
                    );

                    equal(
                        Morpheme.utils.stripper.stripScripts('<p onclick="javascript:alert(1);">whatever<span onmouseover="javascript:alert(2);">whatever<\/span>whatever<\/p>'),
                        '<p>whatever<span>whatever<\/span>whatever<\/p>', 
                        "Module Morpheme.utils.stripper.stripScripts, nested html elements both with javascript events"
                    );

                    equal(
                        Morpheme.utils.stripper.stripScripts('whatever<script>alert(2);<\/script><p><a onclick="javascript: alert(1);" class="whatever">whatever<object>whatever<\/object><span onmouseover="javascript:alert(5);"><\/span><\/a> whatever <a onfocus="javascript: alert(1);" target="_blank">whatever<\/a>whatever<\/p>whatever<p>whatever<\/p>'),
                        'whatever<p><a class="whatever">whatever<span><\/span><\/a> whatever <a target="_blank">whatever<\/a>whatever<\/p>whatever<p>whatever<\/p>', 
                        "Module Morpheme.utils.stripper.stripScripts"
                    );

                });

                module("Module Morpheme.utils.url");

                test("Module Morpheme.utils.url.getBlogUrl", function() {
                    expect(9);

                    // force re-read:
                    Morpheme.utils.url.blogurl__ = null;
                    equal(
                        Morpheme.utils.url.getBlogUrl("http://acarro.tumblr.com"),
                        "http://acarro.tumblr.com", 
                        "Module Morpheme.utils.url.getBlogUrl"
                    );

                    // force re-read:
                    Morpheme.utils.url.blogurl__ = null;
                    equal(
                        Morpheme.utils.url.getBlogUrl("http://acarro.tumblr.com/"),
                        "http://acarro.tumblr.com", 
                        "Module Morpheme.utils.url.getBlogUrl"
                    );

                    // force re-read:
                    Morpheme.utils.url.blogurl__ = null;
                    equal(
                        Morpheme.utils.url.getBlogUrl("http://acarro.tumblr.com?what-ever"),
                        "http://acarro.tumblr.com", 
                        "Module Morpheme.utils.url.getBlogUrl"
                    );

                    // force re-read:
                    Morpheme.utils.url.blogurl__ = null;
                    equal(
                        Morpheme.utils.url.getBlogUrl("http://acarro.tumblr.com/post/123"),
                        "http://acarro.tumblr.com", 
                        "Module Morpheme.utils.url.getBlogUrl"
                    );

                    // force re-read:
                    Morpheme.utils.url.blogurl__ = null;
                    equal(
                        Morpheme.utils.url.getBlogUrl("http://acarro.tumblr.com/post/123?what-ever"),
                        "http://acarro.tumblr.com", 
                        "Module Morpheme.utils.url.getBlogUrl"
                    );

                    // force re-read:
                    Morpheme.utils.url.blogurl__ = null;
                    equal(
                        Morpheme.utils.url.getBlogUrl("http://acarro.tumblr.com/tagged/what ever"),
                        "http://acarro.tumblr.com", 
                        "Module Morpheme.utils.url.getBlogUrl"
                    );

                    // force re-read:
                    Morpheme.utils.url.blogurl__ = null;
                    equal(
                        Morpheme.utils.url.getBlogUrl("http://acarro.tumblr.com/tagged/what ever?what-ever"),
                        "http://acarro.tumblr.com", 
                        "Module Morpheme.utils.url.getBlogUrl"
                    );

                    // force re-read:
                    Morpheme.utils.url.blogurl__ = null;
                    equal(
                        Morpheme.utils.url.getBlogUrl("acarro.tumblr.com/whatever"),
                        "http://acarro.tumblr.com", 
                        "Module Morpheme.utils.url.getBlogUrl (this used to be an issue!)"
                    );

                    // force re-read:
                    Morpheme.utils.url.blogurl__ = null;
                    equal(
                        Morpheme.utils.url.getBlogUrl("acarro.tumblr.com/whatever?what-ever"),
                        "http://acarro.tumblr.com", 
                        "Module Morpheme.utils.url.getBlogUrl"
                    );

                });

                test("Module Morpheme.utils.url.getPostId", function() {
                    expect(8);

                    // force re-read:
                    Morpheme.utils.url.postid__ = null;
                    equal(
                        Morpheme.utils.url.getPostId("http://acarro.tumblr.com/post/123"),
                        "123",
                        "Module Morpheme.utils.url.getPostId"
                    );

                    // force re-read:
                    Morpheme.utils.url.postid__ = null;
                    equal(
                        Morpheme.utils.url.getPostId("http://acarro.tumblr.com/post/123/"),
                        "123",
                        "Module Morpheme.utils.url.getPostId"
                    );

                    // force re-read:
                    Morpheme.utils.url.postid__ = null;
                    equal(
                        Morpheme.utils.url.getPostId("http://acarro.tumblr.com/post/123/what-ever"),
                        "123",
                        "Module Morpheme.utils.url.getPostId"
                    );

                    // force re-read:
                    Morpheme.utils.url.postid__ = null;
                    equal(
                        Morpheme.utils.url.getPostId("http://acarro.tumblr.com/post/"),
                        null, 
                        "Module Morpheme.utils.url.getPostId"
                    );

                    // force re-read:
                    Morpheme.utils.url.postid__ = null;
                    equal(
                        Morpheme.utils.url.getPostId("http://acarro.tumblr.com/post"),
                        null, 
                        "Module Morpheme.utils.url.getPostId"
                    );

                    // force re-read:
                    Morpheme.utils.url.postid__ = null;
                    equal(
                        Morpheme.utils.url.getPostId("http://acarro.tumblr.com/"),
                        null, 
                        "Module Morpheme.utils.url.getPostId"
                    );

                    // force re-read:
                    Morpheme.utils.url.postid__ = null;
                    equal(
                        Morpheme.utils.url.getPostId("http://acarro.tumblr.com/POST/123/what-ever"),
                        "123",
                        "Module Morpheme.utils.url.getPostId"
                    );

                    // force re-read:
                    Morpheme.utils.url.postid__ = null;
                    equal(
                        Morpheme.utils.url.getPostId("http://acarro.tumblr.com/post/123?what-ever"),
                        "123",
                        "Module Morpheme.utils.url.getPostId"
                    );

                });

                test("Module Morpheme.utils.url.getTag", function() {
                    expect(5);

                    // force re-read:
                    Morpheme.utils.url.tag__ = null;
                    equal(
                        Morpheme.utils.url.getTag("http://acarro.tumblr.com/tagged/what ever"),
                        "what ever", 
                        "Module Morpheme.utils.url.getTag"
                    );

                    // force re-read:
                    Morpheme.utils.url.tag__ = null;
                    equal(
                        Morpheme.utils.url.getTag("http://acarro.tumblr.com/tagged/what ever?what-ever"),
                        "what ever", 
                        "Module Morpheme.utils.url.getTag"
                    );

                    // force re-read:
                    Morpheme.utils.url.tag__ = null;
                    equal(
                        Morpheme.utils.url.getTag("http://acarro.tumblr.com/tagged/"),
                        null, 
                        "Module Morpheme.utils.url.getTag"
                    );

                    // force re-read:
                    Morpheme.utils.url.tag__ = null;
                    equal(
                        Morpheme.utils.url.getTag("http://acarro.tumblr.com/"),
                        null, 
                        "Module Morpheme.utils.url.getTag"
                    );

                    // force re-read:
                    Morpheme.utils.url.tag__ = null;
                    equal(
                        Morpheme.utils.url.getTag("http://acarro.tumblr.com/TAGGED/what ever"),
                        "what ever", 
                        "Module Morpheme.utils.url.getTag"
                    );
                });


                module("Module Morpheme.utils.tumblrAPI.load");

                test("Morpheme.utils.tumblrAPI.load.Url", function() {
                    expect(1);


                    function TumblrAPI_Read_Url(i) {
                        var timeoutEvent = i;
                        
                        return function() {
                            return timeoutEvent;
                        };
                    }

                    var aux = TumblrAPI_Read_Url(0); // loading 0
                    TumblrAPI_Read_Url(1);           // changing to 1
                    aux();                           // expecting 0 (the initial value)

                    // this is probably trivial and stupid for such obviousness,
                    // but I do want to make sure this is the result no matter the browser and it's version


                    equal(
                        aux(),
                        0,
                        "Morpheme.utils.tumblrAPI.load.Url - for this functionality to work properly, this behaviour is expected"
                    );
                });


                module("Module Morpheme.utils.tumblrAPI.getJsonUrl");

                test("Morpheme.utils.tumblrAPI.getJsonUrl.forforPost", function() {
                    expect(1);
                    equal(
                        Morpheme.utils.tumblrAPI.getJsonUrl.forPost("http://acarro.tumblr.com", "123"),
                        "http://acarro.tumblr.com/api/read/json?id=123",
                        "Morpheme.utils.tumblrAPI.getJsonUrl.forPost"
                    );
                });

                test("Morpheme.utils.tumblrAPI.getJsonUrl.forPosts", function() {
                    expect(1);
                    equal(
                        Morpheme.utils.tumblrAPI.getJsonUrl.forPosts("http://acarro.tumblr.com", 3, 10, "photo"),
                        "http://acarro.tumblr.com/api/read/json?start=3&num=10&type=photo",
                        "Morpheme.utils.tumblrAPI.getJsonUrl.forPosts"
                    );
                });

                test("Morpheme.utils.tumblrAPI.getJsonUrl.forTaggedPosts", function() {
                    expect(1);
                    equal(
                        Morpheme.utils.tumblrAPI.getJsonUrl.forTaggedPosts("http://acarro.tumblr.com", "what ever+and_more"),
                        "http://acarro.tumblr.com/api/read/json?num=50&tagged=what ever and more",
                        "Morpheme.utils.tumblrAPI.getJsonUrl.forTaggedPosts"
                    );
                });

            });
        </script>
    </head>
    <body>
        <h1 id="qunit-header">QUnit example</h1>
        <h2 id="qunit-banner"></h2>
        <div id="qunit-testrunner-toolbar"></div>
        <h2 id="qunit-userAgent"></h2>
        <ol id="qunit-tests"></ol>
        <div id="qunit-fixture">test markup, will be hidden</div>
    </body>
</html>